package com.manage.config;

import com.manage.module.entity.User;
import com.manage.utils.CacheUtils;
import com.manage.utils.RequestSession;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 拦截器处理类
 * </p>
 *
 */
public class InterceptorConfig implements HandlerInterceptor {
    // 处理拦截请求
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        RequestSession<User> requestSession = new RequestSession<>();
//        // 判断用户是否处于登录状态
//        Cookie[] cookies = request.getCookies();
//        for(Cookie cookie : cookies) {
//            if(cookie.getName().equals("u_id")) {
//                if(cookie.getValue().equals(requestSession.getSessionId())) {
//                    // 判断服务器端sessionId与客户端id相同则放行
//                    // 判断用户是否登录
//                    if(requestSession.getUser() == null){
//                        continue;
//                    }
//                    if(!ObjectUtils.isEmpty(CacheUtils.getCache(requestSession.getUser().getId()))) {
//                        return  true;
//                    }
//                }
//            }
//        }
        //todo 因为没有前端项目保存cookies，所以放开登录状态拦截
        if(true){
            return  true;
        };
        // 两个sessionId不同则返回403
        // 403 表示资源拒绝访问
        response.setStatus(403);
        return false;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
    }
}
